package com.ubtechinc.cruzr.launcher2.model.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.model.entity.AppInfo;
import com.ubtechinc.cruzr.launcher2.model.db.DbHelper;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;

import java.sql.SQLException;
import java.util.List;

public class FilterDao {
    private Dao<AppInfo, Integer> dao;

    public static class DaoHolder {
        public static FilterDao mDao = new FilterDao(LauncherApplication.getContext());
    }

    public static FilterDao get() {
        return DaoHolder.mDao;
    }

    public FilterDao(Context context) {
        DbHelper dbHelper = OpenHelperManager.getHelper(context, DbHelper.class);
        try {
            dao = dbHelper.getDao(AppInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addAppInfo(AppInfo bean) {
        try {
            dao.create(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updateAppInfo(AppInfo bean) {
        try {
            dao.update(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public int deleteAppInfo(AppInfo bean) {
        try {
            LogUtils.e("删除bean = " + bean.activityname + " id = " + bean.id);
            return dao.delete(bean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAppInfo(String url) {
        try {
            dao.deleteBuilder().where().eq("key", url);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteAppInfoList(List<AppInfo> list) {
        try {
            int ret = dao.delete(list);
            Log.e("rqh", "rqh deleteAppInfoList list = " + list.size() + " " + ret);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void addAppInfoList(List<AppInfo> list) {
        try {
            dao.create(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public AppInfo getAppInfoForId(int id) {
        try {
            return dao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<AppInfo> getAllAppInfo() {
        try {
            return dao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public AppInfo getAppInfo(String activity) {
        try {
            List<AppInfo> modelInfos = dao.queryForEq("activityname", activity);
            if (modelInfos != null && !modelInfos.isEmpty()) {
                return modelInfos.get(0);
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void createOrUpdateFilteList(List<AppInfo> list) throws SQLException {
        for (AppInfo info : list) {
            Dao.CreateOrUpdateStatus ret = dao.createOrUpdate(info);
            LogUtils.e("插入是否成功 " + ret.getNumLinesChanged() + "  " + ret.isCreated()  + " " + info.activityname);
        }
    }

    public boolean createOrUpdate(AppInfo info){
        try {
           return dao.createOrUpdate(info).isCreated();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
